package gui.customizer.panes.dhcp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.miginfocom.swing.MigLayout;
import core.models.DhcpClient;
import core.models.DhcpConfiguration;
import core.models.DhcpSubnet;

public class Clients extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1993447210350719818L;
	private JPanel subnetPanel;
	private JComboBox cmbSubnet;
	private JList listClients;
	private JScrollPane clientsScroller;
	private JButton btnAdd;
	private JButton btnDelete;
	private ClientEditor clientEditor;
	private JScrollPane clientEditorScroller;
	private DhcpConfiguration configuration;

	/**
	 * Create the panel.
	 */
	public Clients(DhcpConfiguration conf, SaveAndSend saveAndSend) {
		setLayout(new MigLayout("", "[grow][grow][]", "[][][][grow][grow]"));
		this.configuration = conf;
		configuration.addListDataListener(new ListDataListener() {
			
			@Override
			public void intervalRemoved(ListDataEvent e) {
				if (cmbSubnet.getSelectedIndex() == -1) {
					clientEditor.clearAll();
					listClients.setListData(new Object[0]);
					cmbSubnet.setSelectedIndex(-1);
				}
			}
			@Override
			public void intervalAdded(ListDataEvent e) { }
			@Override
			public void contentsChanged(ListDataEvent e) { }
		});
		
		// Subnet chooser
		subnetPanel = new JPanel();
		add(subnetPanel, "cell 0 0,grow");
		subnetPanel.setLayout(new MigLayout("", "[grow]", "[]"));
		subnetPanel.setBorder(BorderFactory.createTitledBorder("Subnet"));
		
		cmbSubnet = new JComboBox();
		cmbSubnet.setModel(configuration);
		cmbSubnet.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					DhcpSubnet subnet = (DhcpSubnet) cmbSubnet.getSelectedItem();
					listClients.setModel(subnet);
				}
			}
		});
		subnetPanel.add(cmbSubnet, "cell 0 0,alignx left");
		
		// Save subnet
		add(saveAndSend, "cell 1 0 2 1,grow");
			
		// List and buttons
		listClients = new JList();
		listClients.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listClients.setLayoutOrientation(JList.VERTICAL);
		
		clientsScroller = new JScrollPane(listClients);
		clientsScroller.setBorder(BorderFactory.createTitledBorder("Clients"));
		add(clientsScroller, "cell 0 1 2 3,grow");
		
		btnAdd = new JButton("Add");
		add(btnAdd, "cell 2 1,growx");
		
		btnDelete = new JButton("Delete");
		add(btnDelete, "cell 2 2,growx");
		
		// Clients Editor
		clientEditor = new ClientEditor();
		clientEditorScroller = new JScrollPane(clientEditor);
		clientEditorScroller.setBorder(BorderFactory.createTitledBorder("Edit Client"));
		add(clientEditorScroller, "cell 0 4 3 1,grow");
		
		// Listeners
		listClients.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (e.getValueIsAdjusting())
					return;
				DhcpClient client = (DhcpClient) listClients.getSelectedValue();
				if (client != null) {
					clientEditor.setClient(client);
				} else {
					clientEditor.hideApplyButton();
				}
			}
		});
		
		clientEditor.addListenerToApplyButton(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int index = listClients.getSelectedIndex();
				if (index != -1) {
					DhcpClient client = clientEditor.getClient();
					DhcpSubnet subnet = (DhcpSubnet) cmbSubnet.getSelectedItem();
					subnet.remove(index);
					subnet.add(index, client);
					listClients.setSelectedValue(client, true);
				}
			}
		});
		
		btnAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (cmbSubnet.getSelectedIndex() != -1) {
					DhcpClient client = clientEditor.getClient();
					DhcpSubnet subnet = (DhcpSubnet) cmbSubnet
							.getSelectedItem();
					subnet.addElement(client);
					listClients.setSelectedValue(client, true);
				}
			}
		});

		btnDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (listClients.getSelectedIndex() != -1 || cmbSubnet.getSelectedIndex() != -1) {
					int index = listClients.getSelectedIndex();
					DhcpSubnet subnet = (DhcpSubnet) cmbSubnet.getSelectedItem();
					subnet.remove(index);
					if (index == subnet.getSize())
						index--;
					listClients.setSelectedIndex(index);
				}
			}
		});
	}
}
